package com.adkfp.interceptor;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import com.adkfp.bean.BaseBean;

/**
 * class name:PageInterceptor <BR>
 * class description: 分页拦截器 <BR>
 * Remark: <BR>
 * @version 1.00 2018年10月12日
 * @author JACK)jackwei
 */
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
public class PageInterceptor implements Interceptor {

	/**
	 * @Override
	 * Method name: intercept <BR>
	 * Description: 拦截器主要实现方法 <BR>
	 * Remark: <BR>
	 * @param arg0
	 * @return
	 * @throws Throwable  <BR>
	*/
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
	    MetaObject metaObject = MetaObject.forObject(statementHandler,
	    		SystemMetaObject.DEFAULT_OBJECT_FACTORY,
	        SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
	        new DefaultReflectorFactory());
	    MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
	    //通过MetaObject元数据取得方法名id：com.XXX.XXXMapper.方法
	    String id = mappedStatement.getId();
	    //匹配在mybatis中定义的与分页有关的查询id--分页方法
	    if (id.endsWith("Condition")) {
	      //BoundSql中有原始的sql语句和对应的查询参数
	      BoundSql boundSql = statementHandler.getBoundSql();
	      //获取参数对象
	      Object params = boundSql.getParameterObject();
	      //设置参数对象赋值给父类,进行sql语句参数修改
	      BaseBean page = (BaseBean) params;
	      //设置开始/结束页--分页操作[根据你的需求实际操作]
	      //首先要判空!!!!!!
         if (page!=null) {
	    	  if(page.getLimit()!=null&&page.getPage()!=null) {
	    		  int before = page.getLimit() * (page.getPage() - 1);  
	    		  int after = page.getLimit();
	    		  page.setPage(before);
	    		  page.setPageSize(after);
	    	  }
	      }
	    }
	    return invocation.proceed();
	}

	/**
	 * @Override
	 * @see org.apache.ibatis.plugin.Interceptor#plugin(java.lang.Object) <BR>
	 * Method name: plugin <BR>
	 * Description: please write your description <BR>
	 * Remark: <BR>
	 * @param arg0
	 * @return  <BR>
	*/

	@Override
	public Object plugin(Object target) {
		// 如果将拦截器类比喻为代购票的公司，那this就是代购业务员
		//（进入方法前是无代理购票能力业务员，进入后成为有代理能力的业务员）
	    // 通过注解获取拦截目标的信息，如果不符合拦截要求就返回原目标，如果符合则使用动态代理生成代理对象
	    return Plugin.wrap(target, this);
	}

	/**
	 * @Override
	 * @see org.apache.ibatis.plugin.Interceptor#setProperties(java.util.Properties) <BR>
	 * Method name: setProperties <BR>
	 * Description: please write your description <BR>
	 * Remark: <BR>
	 * @param arg0  <BR>
	*/

	@Override
	public void setProperties(Properties arg0) {
		// TODO Auto-generated method stub

	}

}